/*
 *  Copyright (C) 2011 Ryan Sullivan.  Contact: ryan@kayoticlabs.com
 * 
 *  This file is part of kayotic-vkm.
 * 
 *  kayotic-vkm is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  kayotic-vkm is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with kayotic-vkm.  If not, see <http://www.gnu.org/licenses/>.
 */

package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Ryan Sullivan
 */
public class ClientServerThread extends Thread{
    private boolean serverOn = true;
    private int socket;
    private CommandClient client;
    
    public ClientServerThread(int newsocket) throws Exception{
        socket = newsocket;
        //client = newClient;
        //client.sendToServer("0:SetClientServerSocket:" + socket);
        this.start();
    }

    public void run(){
        serverOn = true;
        try{
            ServerSocket welcomeSocket = new ServerSocket(socket);
            while(serverOn){
                /* Get Input Setup Output */
                Socket connectionSocket = welcomeSocket.accept();
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                String[] message = inFromClient.readLine().split(":");

                /* Figure out what kind of command it is */
                switch(Integer.parseInt(message[0])){
                    /* Server Command */
                    case 0:
                        System.out.println("System:"+message[1]);
                        break;

                    case 1:
                        System.out.println(message[1]);
                        break;
                }
            }
        }
        catch(Exception e){
            System.out.println("Thread Error:"+e.getMessage());
        }
    }
}